﻿using System;

namespace NothingButBank
{
    public class AccountHolder
    {
        private string name;
        private Account account;

        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        public Account Account
        {
            get { return account; }
            set { account = value; }
        }

        public AccountHolder(string accountName)
        {
            name = accountName;
        }

        public void AddAccount(Account newAccount)
        {
            if (account == null)
            {
                account = newAccount;
            }
            else
            {
                throw new InvalidOperationException("An account holder may only have one account");
            }
        }

        public double GetCurrentBalance()
        {
            return account.CurrentBalance;
        }

        public void Transfer(DateTime dateTime, AccountHolder toAccountHolder, double transferAmount)
        {
            if (Account == null || toAccountHolder.Account == null)
            {
                throw new InvalidOperationException("both accounts must exist");
            }

            if (GetCurrentBalance() < transferAmount)
            {
                throw new InvalidOperationException("Can't transfer more than current balance");
            }

            account.Withdraw(dateTime, transferAmount);
            toAccountHolder.Account.Deposit(dateTime, transferAmount);
        }
    }
}
